const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_time:"请选择订单日期",//订单日期
    mindate:Math.round((new Date() - 365*86400*1000)),
    p_name:"",//项目名称
    delivery_time:"请选择交货日期",//交货日期
    order_no:"",//订单号
    delivery_address:"",//交货地址
    id:"",//id
    show:false,
    date:"",
    showstarttime:false,
    showlasttime:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this 
    var params = {
      id:options.p_no,
    }
    // that.setData({p_no:options})
    request.requestGet('order/edit_row_info', params, '',function (res) {
      that.setData({
        order_time:res.data.order.order_time,
        delivery_time:res.data.order.delivery_time,
        delivery_address:res.data.order.delivery_address,
        p_no:res.data.order.p_no,
        id:options.p_no,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  submit(event){
    var that = this
    if(that.data.order_time === ""){
      wx.showToast({
        icon: "error",
        title: `请输入订单号`
      });
      return false;
    }else if(that.data.delivery_time === ""){
      wx.showToast({
        icon: "error",
        title: `请输入交货日期`
      });
      return false;
    }else if(that.data.order_time === ""){
      wx.showToast({
        icon: "error",
        title: `请输入订单日期`
      });
      return false;
    }else if(that.data.delivery_address === ""){
      wx.showToast({
        icon: "error",
        title: `请输入交货地址`
      });
      return false;
    }

    let params = {
      id:that.data.id,
      order_time:that.data.order_time,
      delivery_time:that.data.delivery_time,
      delivery_address:that.data.delivery_address,
      p_no:that.data.p_no,
    }
    request.requestAjax('order/edit_row_info',params,"提交中",function(res){
      if(res.status==200){
        const pages = getCurrentPages();
        if (pages.length >= 2) {

          // wx.showToast({
          //   title: '提交成功',
          //   icon:"none",
          //   duration: 2000
          // })

          const prevPage = pages[pages.length - 2];
          pages[pages.length - 2].setData({
            __returnParams: true
           
          }, () => {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
            })
            setTimeout(() => {
              wx.navigateBack() // 确保setData回调完成后再返回
              prevPage.getList(); // 触发上一页自定义刷新方法
            }, 1000)
            
          })
        }
          return false;
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }

  },function(res){
    wx.showToast({
      title: '请求失败',
      icon:"none"
    })
  });
  },
  bindKeyInputVant(event){
    console.log(event);
    let valueName = event.target.dataset.variable;
    this.setData({
      [valueName]:event.detail
    });
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ showstarttime: false });
    this.setData({ showlasttime: false });
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  showstart() {
    this.setData({ showstarttime: true });
  },
  
  showlast() {
    this.setData({ showlasttime: true });
  },

  start(event) {
    const tmptime = this.formatDate(event.detail)
    this.setData({
      order_time:tmptime
    })
    this.onClose()
  },

  last(event) {
    const tmptime = this.formatDate(event.detail)
    this.setData({
      delivery_time:tmptime
    })
    this.onClose()
  },
})